package reduce.fz.com.reduce.util;

import android.content.Context;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import reduce.fz.com.reduce.util.base.ReduceApplication;
import reduce.fz.com.reduce.util.http.ReduceRequest;

/**
 * feel api 按规则生成
 *
 * @author lipf
 */
public class ApiUtil {

    /**
     * 获取api url
     *
     * @param c
     * @param apiUri
     * @param formatArgs
     * @return
     */
    public static String getApi(Context c, int apiUri, Object... formatArgs) {
        String[] paths = null;
        if (c == null) {
            // paths=new String[]{"http://123.57.205.140:8080/"};
            paths = ReduceApplication.getInstance().getResources().getStringArray(apiUri);
        } else {
            paths = c.getResources().getStringArray(apiUri);
        }

        StringBuilder sb = new StringBuilder();
        for (String s : paths) {
            sb.append(s);
        }
        String url = sb.toString();
        int len = formatArgs.length;
        if (len > 0) {
            List<Object> args = new ArrayList<Object>();
            for (int i = 0; i < len; i++) {
                Object arg = formatArgs[i];
                if (arg instanceof String) {
                    try {
                        String a = URLEncoder.encode((String) arg, "UTF-8");
                        args.add(a);
                    } catch (Exception e) {
                        args.add(arg);
                    }
                } else {
                    args.add(arg);
                }
            }
            url = String.format(url, args.toArray(new Object[]{}));
        }
        return url;
    }

    /**
     * 转化map成url参数
     *
     * @param url
     * @param params
     * @return
     */
    public static String parseUrlParams(String url, Map<String, ?> params) {
        if (url.indexOf("?") > -1) {
            url += "&" + urlEncodeUTF8(params);
        } else {
            url += "?" + urlEncodeUTF8(params);
        }
        return url;
    }

    /**
     * 将map编码成url参数格式
     *
     * @param map
     * @return
     */
    public static String urlEncodeUTF8(Map<?, ?> map) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            if (sb.length() > 0) {
                sb.append("&");
            }
            sb.append(String.format("%s=%s",
                    urlEncodeUTF8(entry.getKey().toString()),
                    urlEncodeUTF8(entry.getValue().toString())
            ));
        }
        return sb.toString();
    }

    /**
     * 字符串utf-8编码
     *
     * @param s
     * @return
     */
    public static String urlEncodeUTF8(String s) {
        try {
            return URLEncoder.encode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new UnsupportedOperationException(e);
        }
    }

    public static String getApiWithPage(int apiUri, Context c, int page, int pageSize, Object... formatArgs) {
        String[] paths = c.getResources().getStringArray(apiUri);
        StringBuilder sb = new StringBuilder();
        for (String s : paths) {
            sb.append(s);
        }
        String url = sb.toString();
        int len = formatArgs.length;
        if (len > 0) {
            List<Object> args = new ArrayList<Object>();
            for (int i = 0; i < len; i++) {
                Object arg = formatArgs[i];
                if (arg instanceof String) {
                    try {
                        String a = URLEncoder.encode((String) arg, "UTF-8");
                        args.add(a);
                    } catch (Exception e) {
                        args.add(arg);
                    }
                } else {
                    args.add(arg);
                }
            }
            url = String.format(url, args.toArray(new Object[]{}));
        }
        if (url.indexOf("?") > -1) {
            url += "&page=" + page + "&page_size=" + pageSize;
        } else {
            url += "?page=" + page + "&page_size=" + pageSize;
        }
        return url;
    }

    public static String getApiWithPage(int apiUri, Context c, int page, Object... formatArgs) {
        String[] paths = c.getResources().getStringArray(apiUri);
        StringBuilder sb = new StringBuilder();
        for (String s : paths) {
            sb.append(s);
        }
        String url = sb.toString();
        int len = formatArgs.length;
        if (len > 0) {
            List<Object> args = new ArrayList<Object>();
            for (int i = 0; i < len; i++) {
                Object arg = formatArgs[i];
                if (arg instanceof String) {
                    try {
                        String a = URLEncoder.encode((String) arg, "UTF-8");
                        args.add(a);
                    } catch (Exception e) {
                        args.add(arg);
                    }
                } else {
                    args.add(arg);
                }
            }
            url = String.format(url, args.toArray(new Object[]{}));
        }
        if (url.indexOf("?") > -1) {
            url += "&page=" + page;
        } else {
            url += "?page=" + page;
        }
        return url;
    }


}
